import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../../../common/entity/base.entity';
import { User } from '../../user/entities/user.entity';

@Entity('files')
@Index(['uploaderId'])
@Index(['isPublic'])
@Index(['uploadType'])
export class File extends BaseEntity {
  @Column({
    name: 'original_name',
    type: 'varchar',
    length: 255,
    comment: '原始文件名',
  })
  originalName: string;

  @Column({
    name: 'file_name',
    type: 'varchar',
    length: 255,
    comment: '存储文件名',
  })
  fileName: string;

  @Column({
    name: 'file_path',
    type: 'varchar',
    length: 500,
    comment: '文件存储路径',
  })
  filePath: string;

  @Column({
    name: 'file_size',
    type: 'bigint',
    comment: '文件大小（字节）',
  })
  fileSize: number;

  @Column({
    name: 'mime_type',
    type: 'varchar',
    length: 100,
    comment: 'MIME类型',
  })
  mimeType: string;

  @Column({
    name: 'file_extension',
    type: 'varchar',
    length: 20,
    comment: '文件扩展名',
  })
  fileExtension: string;

  @Column({
    name: 'upload_type',
    type: 'varchar',
    length: 50,
    default: 'local',
    comment: '上传类型（local, cloud等）',
  })
  uploadType: string;

  @Column({
    name: 'download_count',
    type: 'int',
    default: 0,
    comment: '下载次数',
  })
  downloadCount: number;

  @Column({
    name: 'is_public',
    type: 'boolean',
    default: false,
    comment: '是否公开访问',
  })
  isPublic: boolean;

  @Column({
    name: 'uploader_id',
    type: 'varchar',
    length: 36,
    comment: '上传者ID',
  })
  uploaderId: string;

  @Column({
    name: 'description',
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '文件描述',
  })
  description?: string;

  @Column({
    name: 'tags',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '文件标签',
  })
  tags?: string;

  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'uploader_id' })
  uploader: User;
}
